<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Parse_betclick_xml extends CI_Controller {
    function __construct(){
        parent::__construct();
        $this->load->model('game');
    }
    public function index(){
        $url = "http://xml.betclick.com/odds_en.xml";
        $path = "resources/xml/name_time.xml";
        $this->downloadFile($url, $path);
        $this->load->helper('url');
        $completeurl = base_url() . $path;
        $xml = simplexml_load_file($completeurl);
        $i = 1;
        foreach($xml->sport as $sport){
            if($sport->attributes()->name == "Football"){
                foreach($sport->event as $event){
                    foreach($event->match as $match){
                        $names = explode(' - ', $match->attributes()->name);
                        $timestamp = '';
                        if(count($names) == 2){
                            $homeTeam = $this->getSynonyme($names[0]);
                            $awayTeam = $this->getSynonyme($names[1]);
                            $timestamp = str_replace("T", " ", $match->attributes()->start_date);
                            if($homeTeam != '' && $awayTeam != ''){
                                $this->insertTimesInProsoccerTable($homeTeam, $awayTeam, $timestamp);
                            }
                            echo "<p>" .$i. ") " . $match->attributes()->name . " | " . $homeTeam . " - " . $awayTeam . " ( " . $timestamp . " ) " . "</p>";
                        }
                        
                        $i = $i + 1;
                    }
                }
            }
        }
    }
    private function getSynonyme($name){
        $synonyme = $this->game->getGameParticipantSynonyme($name); //poziva se metoda koja vraca sinonim za dati naziv kluba
        return $synonyme;
        //return $name;
    }
    private function insertTimesInProsoccerTable($homeTeam, $awayTeam, $timestamp){
        if($homeTeam != null && $homeTeam != '' && $awayTeam != null && $awayTeam != '' && $timestamp != null && $timestamp != ''){
            $this->game->updateTimesInProsoccerTable($homeTeam, $awayTeam, $timestamp);
        }
        //update-uje prosoccer game_new tabelu datumom pocetka mecha. 
        //Jedan od kriterijuma je da rezultat bude nepoznat - 
        //sto znaci da nije igra jos uvek pocela, 
        //to i nema svrhu za prikazivanje ponude kladjenja, 
        //nego vise da ne bi update-ovali davno zavrsene tekme
    }
    private function downloadFile($url, $path){
        $xml = $this->curl($url);
        file_put_contents($path, $xml);
    }
    private function curl($url){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        $headers = array("text/html","application/xhtml+xml","application/xml","q=0.9,*/*;q=0.8");
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        $agent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11";
        curl_setopt($ch, CURLOPT_USERAGENT, $agent);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_REFERER, 'http://www.google.com');
        curl_setopt($ch, CURLOPT_AUTOREFERER, true);
        $data = curl_exec($ch);
        var_dump($data);
        curl_close($ch);
        return $data; 
    }
}
?>
